<template>
  <el-form
    ref="deptForm"
    label-width="120px"
    :model="form"
    :rules="rulesDeptForm"
  >
    <el-form-item label="部门名称" prop="name">
      <el-input
        v-model="form.name"
        style="width: 80%"
        placeholder="1-50个字符"
      />
    </el-form-item>
    <el-form-item label="部门编码" prop="code">
      <el-input
        v-model="form.code"
        style="width: 80%"
        placeholder="1-50个字符"
      />
    </el-form-item>
    <el-form-item label="部门负责人" prop="manager">
      <el-select v-model="form.manager" style="width: 80%" placeholder="请选择">
        <el-option v-for="item in head" :key="item.id" :value="item.username" />
      </el-select>
    </el-form-item>
    <el-form-item label="部门介绍" prop="introduce">
      <el-input
        v-model="form.introduce"
        style="width: 80%"
        placeholder="1-300个字符"
        type="textarea"
        :rows="3"
      />
    </el-form-item>
    <el-form-item>
      <el-button type="primary" size="small" @click="hSubmit">确定</el-button>
      <el-button size="small" @click="hCancel">取消</el-button>
    </el-form-item>
  </el-form>
</template>

<script>
import { getEmployeeSimple } from '@/api/employees'
import {
  addDepartments,
  getDepartDetail,
  updateDepartments
} from '@/api/departments'
export default {
  props: {
    // eslint-disable-next-line vue/prop-name-casing
    DepartmentId: {
      required: true,
      type: String
    },
    isEdit: {
      type: Boolean,
      required: true
    },
    originList: {
      type: Array,
      required: true
    }
  },
  data() {
    // 部门编号非重验证
    const validCode = (rule, value, callback) => {
      let existCodeList = this.originList.map((item) => item.code)
      if (this.isEdit) {
        const code = this.originList.find(
          (item) => item.id === this.DepartmentId
        ).code
        existCodeList = this.originList
          .filter((item) => item.code !== code)
          .map((item) => item.code)
      }
      existCodeList.includes(value)
        ? callback(new Error(value + '部门编号被占用'))
        : callback()
    }
    // 部门名称非重验证
    const validName = (rule, value, callback) => {
      let existNameList = this.originList
        .filter((item) => item.pid !== this.DepartmentId)
        .map((item) => item.name)
      console.log(existNameList)

      if (this.isEdit) {
        const pid = this.originList.find(
          (item) => item.id === this.DepartmentId
        ).pid
        existNameList = this.originList
          .filter((item) => item.pid === pid && item.id !== this.DepartmentId)
          .map((item) => item.name)
      }
      console.log(existNameList)
      existNameList.includes(value)
        ? callback(new Error(value + '部门编号被占用'))
        : callback()
    }
    return {
      form: {
        name: '', // 部门名称
        code: '', // 部门编码
        manager: '', // 部门管理者
        introduce: '' // 部门介绍
      },
      // 存储负责人信息
      head: [],
      // 表单校验
      rulesDeptForm: {
        name: [
          { required: true, message: '部门名称必填', trigger: 'blur' },
          {
            min: 1,
            max: 50,
            message: '部门名称在1~50字符之间',
            trigger: 'blur'
          },
          {
            validator: validName,
            trigger: 'blur'
          }
        ],
        code: [
          { required: true, message: '部门编码必填', trigger: 'blur' },
          {
            min: 1,
            max: 50,
            message: '部门编码在1~50字符之间',
            trigger: 'blur'
          },
          {
            validator: validCode,
            trigger: 'blur'
          }
        ],
        manager: [
          { required: true, message: '部门管理者必填', trigger: 'blur' }
        ],
        introduce: [
          { required: true, message: '部门介绍必填', trigger: 'blur' },
          {
            min: 1,
            max: 50,
            message: '部门介绍在1~300字符之间',
            trigger: 'blur'
          }
        ]
      }
    }
  },
  created() {
    this.addDepartment()
  },
  methods: {
    hSubmit() {
      this.$refs.deptForm.validate((valid) => {
        if (!valid) return
        this.isEdit ? this.UpdateDepartments() : this.AddDepartments()
      })
    },
    // 实现添加功能
    async AddDepartments() {
      const obj = { ...this.form, pid: this.DepartmentId }
      const res = await addDepartments(obj)
      this.$message.success(res.message)
      // 子传父关闭对话框
      this.$emit('flag')
    },
    // 实现编辑功能
    async UpdateDepartments() {
      const res = await updateDepartments(this.form)
      this.$message.success(res.message)
      // 子传父关闭对话框
      this.$emit('flag')
    },
    // 取消关闭对话框和清空数据
    hCancel() {
      this.$emit('flag')
    },
    // 获取负责人信息
    async addDepartment() {
      const res = await getEmployeeSimple()
      //   console.log(res)
      this.head = res.data
    },
    // 获取当前部门信息
    async loadDetail() {
      if (this.isEdit) {
        const res = await getDepartDetail(this.DepartmentId)
        // console.log(res)
        // 赋值给form
        this.form = res.data
      }
    },
    // 重置表单功能
    resetFrom() {
      this.$refs.deptForm.resetFields()
    }
  }
}
</script>

<style></style>
